<!--
疫病信息管理
-->
<template>
  <BaseMainSlot>
    <template v-slot:zhactbar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-save="false"
        @add="add"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save"
      />
      <el-dialog title="疫病信息管理" :visible.sync="adddialog">
        <el-form ref="formRef" :model="formData" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="疫病名称:"
                prop="blightName"
                :rules="[{
                  required: true, message: '请填写疫病名称:', trigger: ['blur','change']
                }]"
              >
                <el-input v-model="formData.blightName" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="疫病描述:"
                prop="descrption"
                :rules="[{
                  required: true, message: '请填写疫病描述:', trigger: ['blur','change']
                }]"
              >
                <el-input v-model="formData.descrption" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item
                label="检疫处理方式:"
                prop="suggest"
                :rules="[{
                  required: true, message: '请填写检疫处理方式:', trigger: ['blur','change']
                }]"
              >
                <el-input v-model="formData.suggest" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer">
          <el-button @click="adddialog=false">取消</el-button>
          <el-button type="primary" @click="addVaccine">确定</el-button>
        </div>
      </el-dialog>
    </template>
    <template v-slot:searchbox>
      <div class="label-input mr-20">
        <span>疫病名称:</span>
        <el-select v-model="value" placeholder="请选择" clearable @focus="getData">
          <el-option v-for="item in vaccinumNames" :key="item" :label="item" :value="item" />
        </el-select>
      </div>
      <div class="search ml-20" @click="query">
        <img src="~@/assets/action-bar-icon/search.png" />
      </div>
    </template>
    <template v-slot:content>
      <el-table
        ref="tableDataRef"
        :data="tableData"
        max-height="calc(100% - 50px)"
        border
        stripe
        highlight-current-row
        @selection-change="handleSelect"
        @current-change="revampTableData"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="55" label="序号" />
        <el-table-column prop="blightName" label="疫病名称" />
        <el-table-column prop="descrption" label="疫病描述" />
        <el-table-column prop="suggest" label="检疫处理方式" />
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="length"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import Pagination from "@/components/Pagination";
import ActionBar from "@/components/ActionBar/index";
import BaseMainSlot from "@/components/BaseMainSlot/baseone.vue";
import portTest from "@/api/basic-info-manage/disease-information-management/index";
import { mymixin } from "@/utils/mixin";
export default {
  name: "DiseaseInformationManagement",
  components: {
    Pagination,
    ActionBar,
    BaseMainSlot
  },
  mixins: [mymixin],
  data() {
    return {
      value: "",
      test: "",
      diseaseInformation: {},
      tableData: [],
      // 新
      adddialog: false,
      length: 0,
      dels: [],
      vaccinumNames: [],
      formData: {
        blightName: "",
        descrption: "",
        suggest: ""
      }
    };
  },
  watch: {
    nowChanceStau(newval) {
      if (newval === "add") {
        this.adduser();
      } else if (newval === "edit") {
        this.adddialog = true;
      } else if (newval === "cancel") {
        this.setCurrent();
      }
    },
    adddialog(newval, oldval) {
      if (!newval) {
        this.nowChanceStau = "";
        this.$refs.formRef.resetFields();
      }
    }
  },
  created() {
    this.pagingQuery();
  },
  methods: {
    adduser() {
      this.adddialog = true;
    },

    // 新增开始

    // 查询
    pagingQuery() {
      portTest
        .postPageQuery({
          page: this.currentPage,
          size: this.pageSize,
          entity: {}
        })
        .then(res => {
          if (res.code === 1) {
            let data = res.data || {};
            this.length = data.totalElements;
            data = data.content || [];
            this.tableData = data;
          }
        });
    },

    // 添加
    addVaccine() {
      if (this.nowChanceStau === "add") {
        this.$refs["formRef"].validate(valid => {
          if (valid) {
            this.formData.organizationNo = sessionStorage.getItem(
              "organizationNo"
            );
            portTest.postAdd(this.formData).then(res => {
              if (res.code === 1) {
                this.adddialog = false;
                this.nowChanceStau = "";
                this.$message.success("添加成功");
                this.pagingQuery();
              } else {
                this.$message.error("添加失败！！！");
                this.formData = {
                  blightName: "",
                  descrption: "",
                  suggest: ""
                };
              }
            });
          } else {
            this.$message.error("请将信息填写完整！！！");
            return false;
          }
        });
      } else if (this.nowChanceStau === "edit") {
        this.$refs["formRef"].validate(valid => {
          if (valid) {
            this.formData.organizationNo = sessionStorage.getItem(
              "organizationNo"
            );
            this.adddialog = false;
            portTest.postEdit(this.formData).then(res => {
              if (res.code === 1) {
                this.adddialog = false;
                this.nowChanceStau = "";
                this.$message.success("修改成功");
                this.pagingQuery();
              } else {
                this.$message.error("修改失败！！！");
                this.formData = {
                  blightName: "",
                  descrption: "",
                  suggest: ""
                };
              }
            });
          } else {
            this.$message.error("请将信息填写完整！！！");
            return false;
          }
        });
      }
    },
    // 修改
    editTableDate() {},

    // 获取更改数据
    revampTableData(row) {
      if (row) {
        this.cancelActive = true;
        this.editActive = true;
        this.formData = JSON.parse(JSON.stringify(row));
      }
    },

    // 选取操作的数据(删除/修改)
    handleSelect() {
      var selectlist = this.$refs.tableDataRef.selection;
      let delID = []; // 收集记录ID
      if (selectlist.length > 0) {
        selectlist.forEach(item => {
          delID.push(item.blightId);
        });
        this.cancelActive = true;
        this.delActive = true;
      } else {
        this.cancelActive = false;
        this.delActive = false;
      }
      this.dels = delID;
    },

    delmessage() {
      var params = {
        blightIdList: this.dels
      };
      portTest
        .postDel(params)
        .then(res => {
          if (res.code === 1) {
            this.$message.success("删除成功");
            this.pagingQuery();
          }
        })
        .catch(res => {
          this.$message.error("失败");
        });
    },

    getData() {
      portTest.getQueryName().then(res => {
        if (res.code === 1) {
          this.vaccinumNames = res.data;
        }
      });
    },
    query() {
      portTest
        .postPageQuery({
          page: this.currentPage,
          size: this.pageSize,
          entity: {
            blightName: this.value
          }
        })
        .then(res => {
          if (res.code === 1) {
            let data = res.data || {};
            this.length = data.totalElements;
            data = data.content || [];
            this.tableData = data;
          }
        });
    },

    // 取消选中
    setCurrent(row) {
      this.$refs.tableDataRef.setCurrentRow(row);
    },

    // 分页功能实现
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.pagingQuery();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.pagingQuery();
    },
    cancel() {
      this.$refs.tableDataRef.setCurrentRow();
      this.$refs.tableDataRef.clearSelection();
      this.nowChanceStau = "";
      this.addActive = true;
      this.cancelActive = false;
      this.editActive = false;
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
::v-deep .el-table {
  overflow: auto;
}
::v-deep .el-dialog__body {
  background: #0085a0;
  .el-input {
    width: 100%;
  }
}
</style>
